<route lang="json5">
	{
	needLogin: true,
	style: {
	navigationStyle: 'default',
	navigationBarTitleText: '设置',
	},
	}
</route>

<script lang="ts" setup>
	import { doLogoutApi, sseCloseApi } from '@/service/my'
	import { useUserStore } from '@/store'
	import { useMessage } from 'wot-design-uni'
	import { checkForUpdate } from '../index/upgrade/upgrade'
	// #ifdef APP-PLUS
	const jpushModule = uni.requireNativePlugin('JG-JPush')
	// #endif

	// #ifdef APP-PLUS
	import { JPushGetRegistrationID } from '@/utils/jpush'
	import { JPushSetAlias } from '@/utils/jpush'
	// #endif
	// 用户信息
	const userStore = useUserStore();
	const registrationID = ref<string>('')
	const message = useMessage()
	const exitLogin = async () => {
		message
			.confirm({
				msg: '确认退出登录',
				title: '提示',
			})
			.then(() => {
				try {
					// 关闭sse连接
					sseCloseApi()
					doLogoutApi()
					// 退出登录 清除用户信息
					useUserStore().clearUserInfo()
					uni.reLaunch({ url: '/pages/login/index' })
				} catch {
					console.log('取消退出登录')
				}
			})
			.catch(() => {
				console.log('点击了取消按钮')
			})
	}

	function getSystemInfoReq() {
		// 获取系统信息
		uni.getSystemInfo({
			success: function (res) {
				versionInfo.value = res.appVersion
			},
		})
	}

	const versionInfo = ref<string>('')
	onLoad(() => {
		getSystemInfoReq()
		getRegistrationID()
		setJPushAlias()
	})

	// App版本检测
	async function checkVersion() {
		const upgrade = await checkForUpdate()
		if (upgrade === true) {
			const platform = uni.getSystemInfoSync().platform // 手机平台
			// 安卓手机弹窗升级
			if (platform === 'android') {
				uni.navigateTo({
					url: '/pages/index/upgrade/upgrade',
				})
			}
			// IOS无法在线升级提示到商店下载
			else {
				uni.showModal({
					title: '发现新版本',
					content: '请到App store进行升级',
					showCancel: false,
				})
			}
		}
	}

	async function getRegistrationID() {
		registrationID.value = await JPushGetRegistrationID()
	}

	async function setJPushAlias() {
		let userId = userStore.userInfo.userId
		console.info('userId---------->' + userId)
		await JPushSetAlias(userId)
	}
</script>

<template>
	<view class="main_container">
		<view class="top_container">
			<!-- 历史学习记录 -->
			<wd-cell is-link to="/pages/my/noticeSetting" size="large" custom-class="menu-item">
				<template #title>
					<view class="cell-title">消息通知设置</view>
				</template>
			</wd-cell>
			<!-- 帮助中心 -->
			<wd-cell is-link to="/pages/my/help" size="large" custom-class="menu-item">
				<template #title>
					<view class="cell-title">帮助中心</view>
				</template>
			</wd-cell>
			<!-- 关于系统 -->
			<wd-cell size="large" custom-class="menu-item" clickable @click="checkVersion">
				<template #title>
					<view class="cell-title">关于系统</view>
				</template>
				<view style="font-size: 14rpx; color: gray;">v {{ versionInfo }}</view>
			</wd-cell>

			<!-- <wd-cell style="margin-top: 15rpx" :title="`JPUSH:${registrationID}`" size="large" custom-class="item" /> -->
		</view>

		<wd-toast />
		<wd-button size="large" class="bottom_btn" @click="exitLogin()">退出登录</wd-button>
	</view>
</template>

<style scoped>
	.main_container {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		height: calc(100vh - 45px);
		background: #f5f5f5;
	}

	.version-name-text {
		font-size: 16rpx;
		color: #3670f7;
	}

	.cell-title {
		font-size: 28rpx;
	}

	.version-code-text {
		display: inline-block;
		padding: 0 4px;
		margin-left: 5px;
		font-size: 10px;
		color: #a8a7a4;
	}

	.top_container {
		margin: 20rpx;
	}

	/* 	.bottom_container {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 150rpx;
		background-color: white;
	} */

	.bottom_btn {
		width: 85%;
		margin-bottom: 120rpx;
		background: linear-gradient(90deg, #20a4ff 2%, #0d53ff 100%) !important;
	}

	.menu-item {
		margin-top: 15rpx;
		font-weight: bold;
		border-radius: 5rpx;
	}

	:deep(.custom-value) {
		position: absolute;
		right: 0;
		height: 20px;
		white-space: nowrap;
		transform: translate(0, -50%);
	}
</style>